//
//  LevelScene.m
//  iSuDoku
//
//  Created by maccctk on 09-7-5.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "LevelScene.h"
#import "MenuScene.h"
#import "Question.h"
#import "GameScene.h"

#define XOFF							30
#define YOFF							30
#define XGAP							26
#define YGAP							26

@implementation LevelScene

- (id)init {
	self = [super init];
	if(self != nil) {
		Sprite *bg = [Sprite spriteWithFile:@"game.png"];
		[bg setPosition:ccp(240, 160)];
		[self addChild:bg z:0];
		
		Sprite *easysp = [Sprite spriteWithFile:@"easy.png"];
		[easysp setRelativeTransformAnchor:NO];
		[easysp setPosition:ccp(330, 200)];
		[self addChild:easysp z:0];
		
		Label *easylb = [Label labelWithString:@"easy" fontName:@"helvetica" fontSize:10];
		[easylb setRGB:GAME_EASY_R :GAME_EASY_G :GAME_EASY_B];
		[easylb setRelativeTransformAnchor:NO];
		[easylb setPosition:ccp(360, 195)];
		[self addChild:easylb z:0];
		
		Sprite *mediumsp = [Sprite spriteWithFile:@"medium.png"];
		[mediumsp setRelativeTransformAnchor:NO];
		[mediumsp setPosition:ccp(330, 180)];
		[self addChild:mediumsp z:0];
		
		Label *mediumlb = [Label labelWithString:@"medium" fontName:@"helvetica" fontSize:10];
		[mediumlb setRelativeTransformAnchor:NO];
		[mediumlb setRGB:GAME_MEDIUM_R :GAME_MEDIUM_G :GAME_MEDIUM_B];
		[mediumlb setPosition:ccp(360, 175)];
		[self addChild:mediumlb z:0];
		
		Sprite *hardsp = [Sprite spriteWithFile:@"hard.png"];
		[hardsp setRelativeTransformAnchor:NO];
		[hardsp setPosition:ccp(330, 160)];
		[self addChild:hardsp z:0];
		
		Label *hardlb = [Label labelWithString:@"hard" fontName:@"helvetica" fontSize:10];
		[hardlb setRelativeTransformAnchor:NO];
		[hardlb setRGB:GAME_HARD_R :GAME_HARD_G :GAME_HARD_B];
		[hardlb setPosition:ccp(360, 155)];
		[self addChild:hardlb z:0];
		
		Sprite *evilsp = [Sprite spriteWithFile:@"evil.png"];
		[evilsp setRelativeTransformAnchor:NO];
		[evilsp setPosition:ccp(330, 140)];
		[self addChild:evilsp z:0];
		
		Label *evillb = [Label labelWithString:@"evil" fontName:@"helvetica" fontSize:10];
		[evillb setRelativeTransformAnchor:NO];
		[evillb setRGB:GAME_EVIL_R :GAME_EVIL_G :GAME_EVIL_B];
		[evillb setPosition:ccp(360, 135)];
		[self addChild:evillb z:0];
				
		Sprite *finishsp = [Sprite spriteWithFile:@"finish.png"];
		[finishsp setRelativeTransformAnchor:NO];
		[finishsp setPosition:ccp(330, 120)];
		[self addChild:finishsp z:0];
		
		Label *finishlb = [Label labelWithString:@"finish" fontName:@"helvetica" fontSize:10];
		[finishlb setRelativeTransformAnchor:NO];
		[finishlb setRGB:GAME_FINISH_R :GAME_FINISH_G :GAME_FINISH_B];
		[finishlb setPosition:ccp(360, 115)];
		[self addChild:finishlb z:0];
		
		[self addChild:[LevelLayer node] z:1];
	}
	return self;
}

@end

@implementation LevelLayer

- (void)get_cmd_xy:(int)n :(int *)x :(int *)y {
	*x = 0;
	*y = (3 - n) * 30;
}

- (void)get_digit_xy:(int)n :(int *)x :(int *)y {
	*x = XOFF + (n % 10) * XGAP;
	*y = YOFF + (9 - n / 10) * YGAP;
}

- (void)initCmd {
	char *cmdList[] = { "next", "prev", "reset", "main", nil };
	int x, y, i;
	
	Menu *menu = [Menu menuWithItems:nil];
	for(i = 0 ; cmdList[i] != nil ; i ++) {
		NSString *str;
		str = [[NSString alloc] initWithFormat:@"%s", cmdList[i]];
		
		citem[i] = [MenuItemFont itemFromString:str target:self selector:@selector(cmdClicked:)];
		Label *label = citem[i].label;
		[label initWithString:str dimensions:CGSizeMake(60,20) alignment:UITextAlignmentLeft fontName:@"Helvetica" fontSize:11];
		[label setRGB:0 :0 :0x80];				
		
		[self get_cmd_xy:i :&x :&y];
		[citem[i] setPosition:ccp(x, y)];
		[menu addChild:citem[i]];
	}
	[menu setPosition:ccp(470, 70)];
	[self addChild:menu z:2];	
}

- (void)initLevel {
	level = 1;
	
	NSString *levelString = [[NSString alloc] initWithFormat:@"Level Page %3d", level];
	levelLabel = [Label labelWithString:levelString fontName:@"Marker Felt" fontSize:11];
	[levelLabel setPosition:ccp(370, 309)];
	[levelLabel setRGB:0 :0 :0x80];
	[self addChild:levelLabel z:0];
}

- (void)initBoard {
	int x, y, i;
	int number;
	
	Menu *menu = [Menu menuWithItems:nil];	
	NSString *str2 = @"999";
	number = [question number];
	
	for(i = 0 ; i < 100 ; i ++) {
		if(level+i > number)
			break;
		
		NSString *str = [[NSString alloc] initWithFormat:@"%d", level+i];
		litem[i] = [MenuItemFont itemFromString:str2 target:self selector:@selector(digitClicked:)];
		Label *label = litem[i].label;
		[label initWithString:str dimensions:CGSizeMake(26,26) alignment:UITextAlignmentCenter fontName:@"Marker Felt" fontSize:11];
		if([question finished:level+i-1])
			[label setRGB:GAME_FINISH_R :GAME_FINISH_G :GAME_FINISH_B];
		else {
			char difficulty = '0';
			char *cur_question = [question get:level+i-1];
			if(cur_question)
				difficulty = cur_question[0];
			
			switch (difficulty) {
				case '1':
					[label setRGB:GAME_EASY_R :GAME_EASY_G :GAME_EASY_B];
					break;
					
				case '2':
					[label setRGB:GAME_MEDIUM_R :GAME_MEDIUM_G :GAME_MEDIUM_B];
					break;
					
				case '3':
					[label setRGB:GAME_HARD_R :GAME_HARD_G :GAME_HARD_B];
					break;
					
				case '4':
					[label setRGB:GAME_EVIL_R :GAME_EVIL_G :GAME_EVIL_B];
					break;
					
				case '0':
					[label setRGB:0 :0 :0];
					break;
					
				default:
					break;
			}
		}
			
		[litem[i] setString:str];

		[self get_digit_xy:i :&x :&y];
		[litem[i] setPosition:ccp(x, y)];
		[menu addChild:litem[i]];
	}
	
	[menu setPosition:ccp(0, 0)];
	[self addChild:menu z:0];
}

- (void)updateBoard {
	int number;
	int i;
	
	number = [question number];
	
	for(i = 0 ; i < 100 ; i ++) {
		if(level+i > number)
			break;
		
		NSString *str = [[NSString alloc] initWithFormat:@"%d", level+i];
		[litem[i] setString:str];
		if([question finished:level+i-1])
			[litem[i].label setRGB:GAME_FINISH_R :GAME_FINISH_G :GAME_FINISH_B];
		else {
			char difficulty = '0';
			char *cur_question = [question get:level+i-1];
			if(cur_question)
				difficulty = cur_question[0];
			
			switch (difficulty) {
				case '1':
					[litem[i].label setRGB:GAME_EASY_R :GAME_EASY_G :GAME_EASY_B];
					break;
					
				case '2':
					[litem[i].label setRGB:GAME_MEDIUM_R :GAME_MEDIUM_G :GAME_MEDIUM_B];
					break;
					
				case '3':
					[litem[i].label setRGB:GAME_HARD_R :GAME_HARD_G :GAME_HARD_B];
					break;
					
				case '4':
					[litem[i].label setRGB:GAME_EVIL_R :GAME_EVIL_G :GAME_EVIL_B];
					break;
					
				case '0':
					[litem[i].label setRGB:0 :0 :0];
					break;
					
				default:
					break;
			}
		}
		
		litem[i].visible = YES;
	}
	
	for( ; i < 100 ; i ++)
		litem[i].visible = NO;
}

- (id)init {
	self = [super init];
	if(self != nil) {
		question = [Question new];
		
		[self initCmd];
		[self initLevel];
		[self initBoard];
	}
	return self;
}

- (void)dealloc {
	NSLog(@"dealloc");
	[Question release];
	[super dealloc];
}

- (void)mainGame {
	NSLog(@"main game");
	
	MenuScene *ms = [MenuScene node];
	[[Director sharedDirector] replaceScene:ms];
	return;	
}

- (void)nextPage {
	int number = [question number];
	if(level + 100 > number)
		return;
	
	level += 100;
	[self updateBoard];
}

- (void)prevPage {
	if(level < 100)
		return;
	
	level -= 100;
	[self updateBoard];
}

- (void)reset {
	[question reset];
	[self updateBoard];
}

- (void)cmdClicked:(id)sender {
	int i;
	
	for(i = 4 ; i >= 0 ; i --)
		if(citem[i] == sender)
			break;
	if(i < 0)
		return;
	
	NSLog(@"cmd item #%d Clicked", i);
	
	switch(i) {
		case 0:
			[self nextPage];
			break;
		case 1:
			[self prevPage];
			break;
		case 2:
			[self reset];
			break;
		case 3:
			[self mainGame];
			break;
	}
}

- (void)digitClicked:(id)sender {
	int number;
	int i;
	
	for(i = 99 ; i >= 0 ; i --)
		if(litem[i] == sender)
			break;
	
	if(i < 0)
		return;
	
	number = [question number];
	if(level+i > number)
		return;
	
	NSLog(@"digit item %d clicked", level+i);
	
	GameScene *gs = [GameScene node];
	[gs setLevel:level+i];
	[[Director sharedDirector] replaceScene:gs];
}

@end